package com.***;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import com.jrzhuxue.student.R;
import com.jrzhuxue.student.common.util.CircleColorDrawable;


/**
 * Copyright (C), 2008-2015, Huawei Tech. Co., Ltd.
 * <p/>
 * Description : 进度指示器
 *
 * @author [pWX273343] 2015年12月2日
 * @version V100R001
 * @since V100R001
 */
@SuppressLint("NewApi")
public class ProgressIndicator extends FrameLayout {
    private FrameLayout rootlayout, thumpParent;
    private GridView gridView;
    private SquareView thump;
    private LayoutParams p;
    private float thumpColume = 1f;
    private int max = 12;
    private int width, height;
    private FrameLayout lowerLayout;
    private boolean thumpFront = false;

    private float indicatorSpace;

    private int thumpRes = -1, thumpColor = -1;
    private int indicatorRes = R.drawable.round_ring_red;
    private Drawable thumpDrawable, indicatorDrawable, backDrawable;

    public ProgressIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public ProgressIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttributeSet(attrs);
        init();
    }

    private void initAttributeSet(AttributeSet attrs) {
        TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.indicator);
        max = t.getInt(R.styleable.indicator_max, 4);
        thumpColume = t.getFloat(R.styleable.indicator_thumpColume, 1);
        thumpDrawable = t.getDrawable(R.styleable.indicator_thump);
        indicatorDrawable = t.getDrawable(R.styleable.indicator_indicator);
        backDrawable = t.getDrawable(R.styleable.indicator_backgroud);
        thumpFront = t.getBoolean(R.styleable.indicator_thumpFront, false);
        indicatorSpace = t.getDimension(R.styleable.indicator_indicatorPadding, 0);
        t.recycle();
    }

    public ProgressIndicator(Context context) {
        super(context);
        init();
    }

    private void init() {
        rootlayout = new FrameLayout(getContext());
        gridView = new GridView(getContext());
        lowerLayout = new FrameLayout(getContext());

        thump = new SquareView(getContext());
        if (thumpDrawable != null) {
            thump.setBackgroundDrawable(thumpDrawable);
        }
        thumpParent = new FrameLayout(getContext());
        int padding = (int) indicatorSpace / 2;
        thumpParent.setPadding(padding, padding, padding, padding);
        thumpParent.addView(thump, -1, -1);

        if (backDrawable != null) {
            lowerLayout.setBackground(backDrawable);
        }

        lowerLayout.addView(thumpParent);

        gridView.setLayoutParams(new LayoutParams(-1, -1, Gravity.CENTER));
        gridView.setSelector(new ColorDrawable());
        gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        rootlayout.addView(lowerLayout);

        if (thumpFront) {
            rootlayout.addView(gridView, 0, new LayoutParams(-1, -2, Gravity.CENTER));
        } else {
            rootlayout.addView(gridView, -1, -2);
        }
        addView(rootlayout);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        if (max == 0) {
            super.onMeasure(0, 0);
            return;
        }
        int gridWidth;
        if (width / max > height) {
            gridWidth = height * max;
        } else {
            gridWidth = width;
        }
        int round = Math.round(gridWidth * thumpColume / max);

        thumpParent.setLayoutParams(new LayoutParams(round, height));

        p = new LayoutParams(gridWidth, gridWidth / max, Gravity.CENTER);

        rootlayout.setLayoutParams(p);

        lowerLayout.setLayoutParams(p);

        gridView.setNumColumns(max);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public float getThumpColume() {
        return thumpColume;
    }

    /**
     * 滑块所占格子数,可以为小数,如3格半
     */
    public void setThumpColume(float thumpColume) {
        this.thumpColume = thumpColume;

    }


    public void setThump(int res) {
        this.thumpRes = res;
        thumpColor = -1;
        thump.setBackgroundResource(res);
    }

    public void setThumpColor(int color) {
        thumpColor = color;
        thumpRes = -1;
        thump.setBackgroundDrawable(new CircleColorDrawable(color));
    }

    public void setThumpDrawable(Drawable drawable) {
        this.thumpDrawable = drawable;
        thump.setBackgroundDrawable(drawable);
    }

    public Drawable getThumpDrawable() {
        return thumpDrawable;
    }

    /**
     * @param indicatorRes
     * @描述:设置每个格子的背景
     * @作者 [pWX273343] 2015年12月2日
     */
    public void setIndicatorRes(int indicatorRes) {
        this.indicatorRes = indicatorRes;
        adapter.notifyDataSetChanged();
    }

    BaseAdapter adapter = new BaseAdapter() {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SquareView squareView;
            if (convertView == null) {
                convertView = squareView = new SquareView(getContext());
                if (indicatorDrawable != null) {
                    squareView.setImageDrawable(indicatorDrawable);
                } else {
                    squareView.setImageResource(indicatorRes);
                }
                int padding = (int) indicatorSpace / 2;
                squareView.setPadding(padding, padding, padding, padding);
            }
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public int getCount() {
            return max;
        }
    };

    /**
     * Copyright (C), 2008-2015, Huawei Tech. Co., Ltd.
     * <p/>
     * Description : 根据宽度适配高度的方块View
     *
     * @author [pWX273343] 2015年12月2日
     * @version V100R001
     * @since V100R001
     */
    public class SquareView extends ImageView {

        public SquareView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        }
    }

    public float getProgress() {
        return mProgress;
    }

    public void setProgress(float progress) {
        setProgress(progress, false);
    }

    public void setProgress(float progress, boolean anim) {
        if (animator != null) {
            animator.cancel();
        }
        if (max <= thumpColume) {
            thumpParent.setTranslationX(0);
            return;
        }

        final float mMaxX = this.p.width - thumpParent.getWidth();
        float newX = progress * mMaxX / (max - thumpColume);
        if (!anim) {
            thumpParent.setTranslationX(newX);
            return;
        }
        float oldX = this.mProgress * mMaxX / (max - thumpColume);
        if (anim) {

            animator = ValueAnimator.ofFloat(oldX, newX);
            animator.setDuration(skipAnimDuring);
            animator.addUpdateListener(new AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float x = (Float) animation.getAnimatedValue();
                    thumpParent.setTranslationX(x);
                    mProgress = x / mMaxX * (max - thumpColume);
                }
            });
            animator.start();
        }
    }

    private float mProgress = 0;
    private long skipAnimDuring = 250;
    ValueAnimator animator;

    public float getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }


    public boolean isThumpFront() {
        return thumpFront;
    }

    public void setThumpFront(boolean thumpFront) {
        this.thumpFront = thumpFront;
        rootlayout.bringChildToFront(thumpFront ? lowerLayout : gridView);
    }

    public float getIndicatorSpace() {
        return indicatorSpace;
    }

    public void setIndicatorSpace(float indicatorSpace) {
        this.indicatorSpace = indicatorSpace;
    }


}